package com.gec.hawaste.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.hawaste.entity.Demand;
import com.gec.hawaste.service.IDemandService;
import com.gec.hawaste.utils.PageInfo;
import com.gec.hawaste.utils.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 客户的需求填写 前端控制器
 * </p>
 *
 * @author chenxys
 * @since 2022-07-01
 */
@Slf4j
@RestController
@RequestMapping("/manager/demand")
public class DemandController {
    @Autowired
    private IDemandService demandService;

    /**
     * 分页查询所有服务需求
     * @author chenxys
     * @param current 当前页
     * @param pageSize 每页显示条数
     * @return com.gec.hawaste.utils.ResultBean<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/select/{current}/{pageSize}")
    public ResultBean<Page> select(@PathVariable("current") Long current,
                                   @PathVariable("pageSize") Long pageSize){
        PageInfo<Demand> pageInfo = demandService.page(new PageInfo<>(current,pageSize));
        log.info("服务需求分页查询");
        pageInfo.setNavigatePage();
        return ResultBean.ok(pageInfo);
    }

    /**
     * 查询单个服务需求
     * @author chenxys
     * @param demandId 服务ID
     * @return com.gec.hawaste.utils.ResultBean<com.gec.hawaste.entity.Demand>
     */
    @GetMapping("/selectOne")
    public ResultBean<Demand> selectOne(@RequestParam(required = true) Long demandId){
        Demand demand = demandService.getById(demandId);
        log.info("查询单个服务需求");
        return ResultBean.ok(demand);
    }

    /**
     * 修改服务需求
     * @author chenxys
     * @param demand 服务需求
     * @return com.gec.hawaste.utils.ResultBean
     */
    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody Demand demand){
        demandService.saveOrUpdate(demand);
        log.info("修改服务需求");
        return ResultBean.ok();
    }
}
